package com.awardsystem.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awardsystem.Service.SCIService;
import com.awardsystem.Service.UserService;
import com.awardsystem.Utils.FileUtils;
import com.awardsystem.entity.SCI;
import com.awardsystem.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@CrossOrigin
public class ArticleController {
    @Autowired
    SCIService sciService;
    @Autowired
    UserService userService;

    @PostMapping("/addArticle")
    @Transactional
    public JSONObject addArticle(@RequestBody JSONObject params) throws ParseException, JsonProcessingException {
        JSONObject ret=new JSONObject();
        String sciName = params.getString("sciname");
//        Date acceptDate = params.getDate("acceptdate");
//        Date publicDate = params.getDate("publicdate");
        LocalDate acceptDate = LocalDate.parse(params.getString("acceptdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate publicDate = LocalDate.parse(params.getString("publicdate"), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String onlineAddress = params.getString("onlineaddress");
        JSONObject corresponding = params.getJSONObject("corresponding");
        JSONArray sciauthors = params.getJSONArray("sciauthors");
        JSONArray funds = params.getJSONArray("funds");
        String file = params.getString("file");
        if(sciService.findByName(sciName)!=null) {
            ret.put("success", false);
            ret.put("exc", "已存在");
            return ret;
        }
        else {
            SCI sci = new SCI();
            sci.setName(sciName);
            sci.setUrl(onlineAddress);
            sci.setCreateTime(acceptDate);
            sci.setUpdateTime(publicDate);
            sci.setFilePath(file);
            //通讯作者
            String authorName = (String) corresponding.get("authorname");
            String authorCompany = (String) corresponding.get("authorcompany");
            User user = new User();
            if (authorCompany == null){
                user = userService.findUserByPersonName(authorName);
            }else {
                user = userService.findByPersonNameAndCorp(authorName,authorCompany);
            }
            if (user == null){
                ret.put("success", false);
                ret.put("exc", "通讯作者不存在");
                return ret;
            }
            sci.setCorrespondUser(user);
            sciService.save(sci);
            //作者
            Collections.sort(sciauthors, new Comparator<Object>(){
                @Override
                public int compare(Object object1, Object object2) {// 实现接口中的方法
                    return ((Map<String, String>)object1).get("key").
                            compareTo(((Map<String, String>)object2).get("key"));
                }
            });
            for (Object sciAuthoro:sciauthors) {
                Map<String, String> sciAuthor = (Map<String, String>)sciAuthoro;
                authorName = sciAuthor.get("authorname");
                if (authorCompany == null){
                    user = userService.findUserByPersonName(authorName);
                }else {
                    user = userService.findByPersonNameAndCorp(authorName,authorCompany);
                }
                if (user == null){
                    ret.put("success", false);
                    ret.put("exc", "作者"+authorName+"不存在");
                    return ret;
                }
                sci.getAuthor().add(user);
            }
            sci.setFunds(funds.toString());
            sciService.save(sci);
            ret.put("success", true);
            ret.put("exc", "success");
            return ret;
        }
    }
}
